package com.zoe.app.config;

import cn.hutool.core.map.MapUtil;
import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.DefaultErrorViewResolver;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author Zoe.Zhang
 * @date 2021/10/07
 * @description 自定义错误页面
 */
@Component
public class MyErrorViewResolver extends DefaultErrorViewResolver {
    /**
     * Create a new {@link DefaultErrorViewResolver} instance.
     *
     * @param applicationContext the source application context
     * @param resources          resource properties
     * @since 2.4.0
     */
    public MyErrorViewResolver(ApplicationContext applicationContext, WebProperties.Resources resources) {
        super(applicationContext, resources);
    }

    @Override
    public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
        Map<String, Object> map = MapUtil.newHashMap();
        map.putAll(model);
        if ((Integer) model.get("status") >= 500) {
            map.put("message", "服务器内部错误-自定义错误视图");
        }
        ModelAndView modelAndView = new ModelAndView("zoe/999", map);
        return modelAndView;
    }
}
